import store from '@/store/index.js'

export function wxLogin(params) {
	return new Promise((resolve, reject) => {

		uni.login({
			provider: 'weixin',
			...params,
			success: res => {
				console.log('login', res)
				resolve(res)
			},
			fail: err => {
				console.error('login', err)
				reject(err)
			}
		})
	})
}
export default function wechatLoign() {
	return new Promise(async (resolve, reject) => {
		const {
			uniPlatform
		} = uni.getSystemInfoSync()
		const data = {
			loginType: 'wechat'
		}
		
		const {
			code
		} = await wxLogin();
		if (uniPlatform === 'mp-weixin') {
			// 微信小程序
			data.wxMiniOpenId = code;
		} else if (uniPlatform === 'app') {
			// APP
			data.wxOpenId = 1;
			data.unionid = 1		
		}
		
		store.dispatch('login', data).then(() => {
			resolve()
		}).catch(() => {
			reject()
		})

	})

}